﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using OCDStudio.SqlAzureBackup.Client.Properties;
using OCDStudio.SqlAzureBackup.Client.Code;

namespace OCDStudio.SqlAzureBackup.Client
{
    public partial class NewConnection : Form
    {
        public BackupProfile Profile { get; set; }

        public NewConnection()
        {
            InitializeComponent();
        }

        private void txtServer_Click(object sender, EventArgs e)
        {
            if (txtServer.Text == "[something].database.windows.net") txtServer.Text = ".database.windows.net";
            
        }

        private bool tested = false;
        private void cmdTestConnection_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtServer.Text) || string.IsNullOrEmpty(txtCatalog.Text)
                || string.IsNullOrEmpty(txtPassword.Text) || string.IsNullOrEmpty(txtUsername.Text)) MessageBox.Show("Missing values");
            else
            {
                if (!BackupUtils.CheckConnection(txtServer.Text, txtCatalog.Text, txtUsername.Text, txtPassword.Text))
                    MessageBox.Show("Connection refused","Attention",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                else
                    MessageBox.Show("Everything is ok...", "Great job", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tested = true;
            }
        }

        private void txtServer_TextChanged(object sender, EventArgs e)
        {
            tested = false;
        }

        private void cmdGo_Click(object sender, EventArgs e)
        {
            if (!tested)
                if (MessageBox.Show("Do you want to test it before exiting?", "Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                    System.Windows.Forms.DialogResult.Yes)
                {
                    cmdTestConnection_Click(null, null);
                    return;
                }
            Profile = new BackupProfile()
            {
                Catalog=txtCatalog.Text,
                Password=txtPassword.Text,
                Server=txtServer.Text,
                User=txtUsername.Text,
                ProfileName=new Random().Next(1000).ToString()
            };
            Dispose();
        }
    }
}
